/**
 * 2011 HuYao
 */
package com.wh.data.pp.bp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.wh.data.model.SumBp;

/**
 * @author HuYao
 * 
 */
public class SumBpTotalTimeProcessor {

    private String filePath = ""; //$NON-NLS-1$

    final private List<SumBp> modelList = new ArrayList<SumBp>();

    /**
     * @param sum_bp_path
     * @throws IOException
     */
    public SumBpTotalTimeProcessor(String sum_bp_path) throws IOException {
	this.filePath = sum_bp_path;
	File file = new File(this.filePath);
	if (!file.exists())
	    throw new FileNotFoundException();
	BufferedReader br = new BufferedReader(new FileReader(file));
	String line = ""; //$NON-NLS-1$
	while ((line = br.readLine()) != null) {
	    StringTokenizer st = new StringTokenizer(line);
	    try {
		int therapist = Integer.parseInt(st.nextToken());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		double total_cost = Double.parseDouble(st.nextToken());
		double travel_time = Double.parseDouble(st.nextToken());
		double treatment_time = Double.parseDouble(st.nextToken());
		double ad_time = Double.parseDouble(st.nextToken());
		double distance = Double.parseDouble(st.nextToken());
		int day = Integer.parseInt(st.nextToken());
		SumBp tmp = new SumBp(therapist, start, end, total_cost,
			travel_time, treatment_time, ad_time, distance, day);
		this.modelList.add(tmp);
		tmp = null;
	    } catch (NumberFormatException e) {
		System.out.println("Skip this line" + line); //$NON-NLS-1$
	    }
	}
	br.close();

    }

    /**
     * @throws IOException
     */
    public void process() throws IOException {
	File file = new File(this.filePath);
	BufferedWriter bw = new BufferedWriter(new FileWriter(file));
	for (SumBp sumbp : this.modelList) {
	    String line = sumbp.getTherapist() + " " + sumbp.getStart() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbp.getEnd() + " " + sumbp.getTotal_cost() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbp.getTotal_time() + " " + sumbp.get_distance() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbp.getDay();
	    bw.write(line);
	    bw.newLine();
	}
	bw.close();
    }

}
